package com.dexgdx.game.util.trajectory;

import com.dexgdx.game.constants.EMoveDirection;
import com.dexgdx.game.structure.base.BasePoint;
import com.dexgdx.game.structure.base.BaseRegion;
import com.dexgdx.game.structure.base.MobileObject;

public class TrajectoryCalculator {

	private Trajectory trajectory;
	
	public TrajectoryCalculator(float speed) {
		trajectory = new Trajectory(speed,  Math.abs(speed), (float) Math.sqrt(2 * (speed * speed)));
	}
	
	public Trajectory calculate(MobileObject movingObject){
		return calculate(movingObject, movingObject.getMoveTarget());
	}
	
	public Trajectory calculate(BaseRegion fromObject, BaseRegion targetRegion){
		float deltaX = targetRegion.getX() - fromObject.getX();
		float deltaY = targetRegion.getY() - fromObject.getY();
		double divider = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
		trajectory.x = (float) ((trajectory.getSpeed() * deltaX) / divider);
		trajectory.y = (float) ((trajectory.getSpeed() * deltaY) / divider);
		trajectory.setDirection(TrajectoryCalculator.findDirection(trajectory));
		return trajectory;
	}

	public Trajectory getTrajectory() {
		return trajectory;
	}
	
	public static EMoveDirection findDirection(BasePoint trajectory){
		EMoveDirection direction = null;
		if(trajectory.x > 0){
			if(trajectory.y > 0){
				direction = EMoveDirection.SOUTH_EAST;
			}
			else if(trajectory.y < 0){
				direction = EMoveDirection.NORTH_EAST;
			}
			else {
				direction = EMoveDirection.EAST;
			}
		}
		else if(trajectory.x < 0){
			if(trajectory.y > 0){
				direction = EMoveDirection.SOUTH_WEST;
			}
			else if(trajectory.y < 0){
				direction = EMoveDirection.NORTH_WEST;
			}
			else {
				direction = EMoveDirection.WEST;
			}
		}
		else {
			if(trajectory.y > 0){
				direction = EMoveDirection.SOUTH;
			}
			else {
				direction = EMoveDirection.NORTH;
			}
		}
		return direction;
	}
}
